package com.atguigu.sparsearry;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-08-08 14:12
 */
public class CircleArrayQueueDemo {
    public static void main(String[] args) {
        //创建一个队列
        CircleArray queue = new CircleArray(4);
        Scanner scanner = new Scanner(System.in);
        char key=' ';
        boolean loop=true;
        //输出一个菜单
        while(loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            //接受一个字符
            key=scanner.next().charAt(0);
            switch (key){
                case 's':queue.showQueue();
                    break;
                case 'a':
                    try {
                        System.out.println("请输入一个数据");
                        int value=scanner.nextInt();
                        queue.addQueue(value);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    //取出数据

                    try {
                        int n = queue.getQueue();
                        System.out.println("取出的数据："+n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    //取出头部的数据
                    try {
                        int res = queue.headQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    System.out.println("退出程序！");
                    break;
            }

        }

    }
}
class CircleArray{
    private int Maxsize;
    private int [] arr;
    private int front;
    private int rear;
//创建构造器
    public CircleArray(int arrMaxSize) {
        Maxsize=arrMaxSize;
        arr = new int[Maxsize];
        front=0;
        rear=0;
    }
    //判断是否为空
    public boolean isEmpty(){
        return rear==front;
    }
    //判断是否为满
    public boolean isFull(){
        return front==(rear+1)%Maxsize;
    }
    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列已经满，不能加入数据！");
            return;
        }
        arr[rear]=n;
        rear=(rear+1)%Maxsize;
    }
    //取出数据
    public int getQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列为空，不能取出数据");
        }
        int value=arr[front];
        front=(front+1)%Maxsize;
        return value;
    }
    //显示队列
    public void showQueue(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空，没有数据");
        }
        for (int i=front;i<front+size();i++){
            System.out.print("arr["+i%Maxsize+"]"+"="+arr[i%Maxsize]);
        }
    }
    //得到当前队列有效数据的个数
    public int size(){
        return (rear-front+Maxsize)%Maxsize;
    }
    //显示头部数据
    public int headQueue(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空，没有数据");
        }
        return arr[front];
    }
}
